package com.ak.server.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ak.common.annotation.LogEndpoint;
import com.ak.common.controller.BaseController;
import com.ak.common.entity.AKResponse;
import com.ak.common.entity.QueryRequest;
import com.ak.common.utils.AKUtil;
import com.ak.server.system.entity.Role;
import com.ak.server.system.service.IRoleService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

@Api(tags = "角色模块")
@Validated
@RestController
@RequestMapping("role")
@RequiredArgsConstructor
public class RoleController extends BaseController {

    private final IRoleService roleService;

    @ApiOperation("获取角色集合")
    @GetMapping
    public AKResponse roleList(QueryRequest queryRequest, Role role) {
        Map<String, Object> dataTable = AKUtil.getDataTable(roleService.findRoles(role, queryRequest));
        return new AKResponse().data(dataTable);
    }

    @ApiOperation("获取所有角色")
    @GetMapping("options")
    public AKResponse roles() {
        List<Role> allRoles = roleService.findAllRoles();
        return new AKResponse().data(allRoles);
    }

    @ApiOperation("检查角色名是否存在")
    @GetMapping("check/{roleName}")
    public boolean checkRoleName(@NotBlank(message = "{required}") @PathVariable String roleName) {
        Role result = this.roleService.findByName(roleName);
        return result == null;
    }

    @ApiOperation("添加角色")
    @PostMapping
    @PreAuthorize("hasAuthority('role:add')")
    @LogEndpoint(operation = "新增角色", exceptionMessage = "新增角色失败")
    public void addRole(@Valid Role role) {
    	// 租户管理员新增角色，所以角色的租户编码为当前登录用户的租户编码
    	role.setCreateBy(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        this.roleService.createRole(role);
    }

    @ApiOperation("删除角色")
    @DeleteMapping("/{roleIds}")
    @PreAuthorize("hasAuthority('role:delete')")
    @LogEndpoint(operation = "删除角色", exceptionMessage = "删除角色失败")
    public void deleteRoles(@NotBlank(message = "{required}") @PathVariable String roleIds) {
        String[] ids = roleIds.split(StringPool.COMMA);
        this.roleService.deleteRoles(ids);
    }

    @ApiOperation("修改角色信息")
    @PutMapping
    @PreAuthorize("hasAuthority('role:update')")
    @LogEndpoint(operation = "修改角色", exceptionMessage = "修改角色失败")
    public void updateRole(@Valid Role role) {
    	role.setUpdateBy(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
        this.roleService.updateRole(role);
    }

    @PostMapping("excel")
    @PreAuthorize("hasAuthority('role:export')")
    @LogEndpoint(operation = "导出角色数据", exceptionMessage = "导出Excel失败")
    public void export(QueryRequest queryRequest, Role role, HttpServletResponse response) {
        List<Role> roles = this.roleService.findRoles(role, queryRequest).getRecords();
        ExcelKit.$Export(Role.class, response).downXlsx(roles, false);
    }
}
